package io.renren.modules.seal.utils;

import cfca.paperless.client.PaperlessClient;
import cfca.paperless.dto.RequestHead;
import io.renren.common.utils.Constants;
import io.renren.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

@Slf4j
public class PaperlessClientFactory {

    private static PaperlessClient paperlessClient;


    static {
        if (paperlessClient == null){
            paperlessClient = new PaperlessClient(PaperlessConfig.host, PaperlessConfig.port, 3000, 20000);

            paperlessClient.setSSL(false);
            paperlessClient.setSslProtocol(PaperlessConfig.sslProtocol);
            paperlessClient.setKeyStorePath(PaperlessConfig.keyStorePath);
            paperlessClient.setKeyStorePassword(PaperlessConfig.keyStorePassword);
            paperlessClient.setTrustStorePath(PaperlessConfig.trustStorePath);
            paperlessClient.setTrustStorePassword(PaperlessConfig.trustStorePassword);
        }
    }

    public static PaperlessClient getPaperlessClient(){
        if (paperlessClient == null){
            paperlessClient = new PaperlessClient(PaperlessConfig.host, PaperlessConfig.port, 3000, 20000);

            paperlessClient.setSSL(false);
            paperlessClient.setSslProtocol(PaperlessConfig.sslProtocol);
            paperlessClient.setKeyStorePath(PaperlessConfig.keyStorePath);
            paperlessClient.setKeyStorePassword(PaperlessConfig.keyStorePassword);
            paperlessClient.setTrustStorePath(PaperlessConfig.trustStorePath);
            paperlessClient.setTrustStorePassword(PaperlessConfig.trustStorePassword);
        }
        return paperlessClient;
    }

    public static RequestHead getHead(){
        String active = Constants.isKj ? "Kj":"Sy";
        String time = DateUtils.dateTimeNow("yyyyMMddHHmmssSSS");
        String transactionNo = active+time;
        log.info("transNo:{}",transactionNo);
        String organizationCode = PaperlessConfig.organizationCode;
        String operatorCode = PaperlessConfig.operationCode;
        String channelCode = PaperlessConfig.channelCode;

        // 请求报文头
        RequestHead requestHeadBean = new RequestHead();
        requestHeadBean.setTransactionNo(transactionNo);
        requestHeadBean.setOrganizationCode(organizationCode);
        requestHeadBean.setOperatorCode(operatorCode);
        requestHeadBean.setChannelCode(channelCode);
        return requestHeadBean;

    }

    public static RequestHead getHead(String appno){
        String transactionNo = appno;
        String organizationCode = PaperlessConfig.organizationCode;
        String operatorCode = PaperlessConfig.operationCode;
        String channelCode = PaperlessConfig.channelCode;

        // 请求报文头
        RequestHead requestHeadBean = new RequestHead();
        requestHeadBean.setTransactionNo(transactionNo);
        requestHeadBean.setOrganizationCode(organizationCode);
        requestHeadBean.setOperatorCode(operatorCode);
        requestHeadBean.setChannelCode(channelCode);
        return requestHeadBean;

    }
}
